package com.zian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.zian.context.BaseContext;
import com.zian.domain.dto.UserAddressDTO;
import com.zian.domain.po.SysUserAddress;
import com.zian.enums.DefaultFlagEnum;
import com.zian.enums.IsDeletedEnum;
import com.zian.exception.BizIllegalException;
import com.zian.exception.DbException;
import com.zian.mapper.SysUserAddressMapper;
import com.zian.service.ISysUserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 收货地址表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Service
public class SysUserAddressServiceImpl extends ServiceImpl<SysUserAddressMapper, SysUserAddress> implements ISysUserAddressService {

    @Override
    public SysUserAddress getDefaultAddress() {
        Long currentId = BaseContext.getCurrentId();
        return lambdaQuery()
                .eq(SysUserAddress::getUserId, currentId)
                .eq(SysUserAddress::getDefaultFlag, DefaultFlagEnum.YES)
                .eq(SysUserAddress::getIsDeleted, IsDeletedEnum.NO)
                .one();
    }

    @Override
    @Transactional
    public void addUserAddress(UserAddressDTO dto) {
        Long currentId = BaseContext.getCurrentId();
        SysUserAddress userAddress = BeanUtil.toBean(dto, SysUserAddress.class);

        // 判断是否为默认地址
        if (dto.getDefaultFlag().equals(DefaultFlagEnum.YES)) {
            // 将所有的 默认地址设置为非默认
            lambdaUpdate()
                    .eq(SysUserAddress::getDefaultFlag, DefaultFlagEnum.YES)
                    .set(SysUserAddress::getDefaultFlag, DefaultFlagEnum.NO)
                    .update();
        }

        userAddress.setUserId(currentId);
        boolean b = save(userAddress);
        if (!b) throw new DbException("新增用户收货地址失败");
    }

    @Override
    public void updateUserAddress(UserAddressDTO dto) {
        Long currentId = BaseContext.getCurrentId();
        SysUserAddress address = lambdaQuery().eq(SysUserAddress::getId, dto.getId()).eq(SysUserAddress::getUserId, currentId).one();
        if (address == null) throw new BizIllegalException("用户收货地址不存在");
        if (!address.getUserId().equals(currentId)) throw new DbException("用户收货地址不存在");

        if (dto.getDefaultFlag().equals(DefaultFlagEnum.YES)) {
            lambdaUpdate()
                    .eq(SysUserAddress::getDefaultFlag, DefaultFlagEnum.YES)
                    .set(SysUserAddress::getDefaultFlag, DefaultFlagEnum.NO)
                    .update();
        }

        address.setDetailAddress(dto.getDetailAddress());
        address.setUserName(dto.getUserName());
        address.setUserPhone(dto.getUserPhone());
        address.setCityName(dto.getCityName());
        address.setProvinceName(dto.getProvinceName());
        address.setRegionName(dto.getRegionName());
        address.setDefaultFlag(dto.getDefaultFlag());

        boolean b = updateById(address);
        if (!b) throw new DbException("修改用户收货地址失败");
    }

    @Override
    public SysUserAddress getUserAddress(Long id) {
        Long currentId = BaseContext.getCurrentId();
        SysUserAddress address = lambdaQuery()
                .eq(SysUserAddress::getId, id)
                .eq(SysUserAddress::getUserId, currentId)
                .eq(SysUserAddress::getIsDeleted, IsDeletedEnum.NO).one();
        if (address == null) throw new BizIllegalException("用户收货地址不存在");
        return address;
    }

    @Override
    public void deleteUserAddress(Long id) {
        Long currentId = BaseContext.getCurrentId();
        SysUserAddress address = lambdaQuery().eq(SysUserAddress::getId, id).eq(SysUserAddress::getUserId, currentId).one();
        if (address == null) throw new BizIllegalException("用户收货地址不存在");
        boolean b = lambdaUpdate()
                .eq(SysUserAddress::getId, id)
                .set(SysUserAddress::getIsDeleted, IsDeletedEnum.YES)
                .set(SysUserAddress::getUpdateTime, LocalDateTime.now())
                .update();
        if (!b) throw new DbException("删除用户收货地址失败");
    }

    @Override
    public List<SysUserAddress> getUserAddressList() {
        Long currentId = BaseContext.getCurrentId();
        List<SysUserAddress> list = lambdaQuery()
                .eq(SysUserAddress::getUserId, currentId)
                .eq(SysUserAddress::getIsDeleted, IsDeletedEnum.NO)
                .list();
        if (list.isEmpty()) return CollUtil.empty(List.class);
        return list;
    }
}
